﻿Imports SdlDotNet.Graphics
Imports SdlDotNet.Graphics.Sprites
Imports SdlDotNet.Input
Imports System.Drawing

Public Class StartMenu
    Implements IMenu

    Private Position As Point

    Private BuildAvailableSurface As New Surface("Resources\Images\Game\Menu\Build_Available.jpg")
    Private BuildSelectedSurface As New Surface("Resources\Images\Game\Menu\Build_Selected.jpg")
    Private PropagandaAvailableSurface As New Surface("Resources\Images\Game\Menu\Propaganda_Available.jpg")
    Private PropagandaSelectedSurface As New Surface("Resources\Images\Game\Menu\Propaganda_Selected.jpg")
    Private DeployWeaponAvailableSurface As New Surface("Resources\Images\Game\Menu\DeployWeapon_Available.jpg")
    Private DeployWeaponUnavailableSurface As New Surface("Resources\Images\Game\Menu\DeployWeapon_Unavailable.jpg")
    Private DeployWeaponSelectedSurface As New Surface("Resources\Images\Game\Menu\DeployWeapon_Selected.jpg")
    Private DeployDefenseAvailableSurface As New Surface("Resources\Images\Game\Menu\DeployDefense_Available.jpg")
    Private DeployDefenseUnavailableSurface As New Surface("Resources\Images\Game\Menu\DeployDefense_Unavailable.jpg")
    Private DeployDefenseSelectedSurface As New Surface("Resources\Images\Game\Menu\DeployDefense_Selected.jpg")
    Private BombAvailableSurface As New Surface("Resources\Images\Game\Menu\Bomb_Available.jpg")
    Private BombUnavailableSurface As New Surface("Resources\Images\Game\Menu\Bomb_Unavailable.jpg")
    Private BombSelectedSurface As New Surface("Resources\Images\Game\Menu\Bomb_Selected.jpg")
    Private LaunchMissileAvailableSurface As New Surface("Resources\Images\Game\Menu\LaunchMissile_Available.jpg")
    Private LaunchMissileUnavailableSurface As New Surface("Resources\Images\Game\Menu\LaunchMissile_Unavailable.jpg")
    Private LaunchMissileSelectedSurface As New Surface("Resources\Images\Game\Menu\LaunchMissile_Selected.jpg")

    Private BuildButton As Sprite
    Private PropagandaButton As Sprite
    Private DeployWeaponButton As Sprite
    Private DeployDefenseButton As Sprite
    Private BombButton As Sprite
    Private LaunchMissileButton As Sprite

    Private CanDeployWeapon As Boolean = False
    Private CanDeployDefense As Boolean = False
    Private CanBomb As Boolean = False
    Private CanLaunchMissile As Boolean = False

    Public Event ClickedBuildButton()
    Public Event ClickedPropagandaButton()
    Public Event ClickedLaunchMissileButton()
    Public Event ClickedDeployWeaponButton()
    Public Event ClickedDeployDefenseButton()
    Public Event ClickedBombButton()

    Public Sub New(ByVal position As Point, ByVal player As Player)
        Me.Position = position
        InitializeSprites()
        ShowMenuForPlayer(player)
    End Sub

    Private Sub InitializeSprites()
        BuildButton = New Sprite(BuildAvailableSurface, New Point(Position.X, Position.Y))
        PropagandaButton = New Sprite(PropagandaAvailableSurface, New Point(Position.X, Position.Y + 19))
        DeployWeaponButton = New Sprite(DeployWeaponUnavailableSurface, New Point(Position.X, Position.Y + (19 * 2)))
        DeployDefenseButton = New Sprite(DeployDefenseUnavailableSurface, New Point(Position.X, Position.Y + (19 * 3)))
        BombButton = New Sprite(BombUnavailableSurface, New Point(Position.X, Position.Y + (19 * 4)))
        LaunchMissileButton = New Sprite(LaunchMissileUnavailableSurface, New Point(Position.X, Position.Y + (19 * 5)))
    End Sub

    Public Sub Draw(ByRef display As Surface) Implements IMenu.Draw
        display.Blit(BuildButton)
        display.Blit(PropagandaButton)
        display.Blit(DeployWeaponButton)
        display.Blit(DeployDefenseButton)
        display.Blit(BombButton)
        display.Blit(LaunchMissileButton)
    End Sub

    Public Sub HandleMouseButtonUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs) Implements IMenu.HandleMouseButtonUp
        If (BuildButton.IntersectsWith(e.Position)) Then
            RaiseEvent ClickedBuildButton()
        ElseIf (PropagandaButton.IntersectsWith(e.Position)) Then
            RaiseEvent ClickedPropagandaButton()
        ElseIf (DeployWeaponButton.IntersectsWith(e.Position) And CanDeployWeapon) Then
            RaiseEvent ClickedDeployWeaponButton()
        ElseIf (DeployDefenseButton.IntersectsWith(e.Position) And CanDeployDefense) Then
            RaiseEvent ClickedDeployDefenseButton()
        ElseIf (BombButton.IntersectsWith(e.Position) And CanBomb) Then
            RaiseEvent ClickedBombButton()
        ElseIf (LaunchMissileButton.IntersectsWith(e.Position) And CanLaunchMissile) Then
            RaiseEvent ClickedLaunchMissileButton()
        End If
    End Sub

    Public Sub HandleMouseMotion(ByVal sender As Object, ByVal e As MouseMotionEventArgs) Implements IMenu.HandleMouseMotion
        ApplyPlayerView()
        If (BuildButton.IntersectsWith(e.Position)) Then
            BuildButton.Surface = BuildSelectedSurface
        ElseIf (PropagandaButton.IntersectsWith(e.Position)) Then
            PropagandaButton.Surface = PropagandaSelectedSurface
        ElseIf (DeployWeaponButton.IntersectsWith(e.Position) And CanDeployWeapon) Then
            DeployWeaponButton.Surface = DeployWeaponSelectedSurface
        ElseIf (DeployDefenseButton.IntersectsWith(e.Position) And CanDeployDefense) Then
            DeployDefenseButton.Surface = DeployDefenseSelectedSurface
        ElseIf (BombButton.IntersectsWith(e.Position) And CanBomb) Then
            BombButton.Surface = BombSelectedSurface
        ElseIf (LaunchMissileButton.IntersectsWith(e.Position) And CanLaunchMissile) Then
            LaunchMissileButton.Surface = LaunchMissileSelectedSurface
        End If
    End Sub

    Public Sub ShowMenuForPlayer(ByRef player As Player) Implements IMenu.ShowMenuForPlayer
        CanDeployWeapon = player.CanDeployWeapon()
        CanDeployDefense = player.CanDeployDefense()
        CanBomb = player.CanBomb()
        CanLaunchMissile = player.CanLaunchMissile()

        HandleMouseMotion(Me, New MouseMotionEventArgs(False, MouseButton.None, CShort(Mouse.MousePosition.X), CShort(Mouse.MousePosition.Y), CShort(Mouse.MousePositionChange.X), CShort(Mouse.MousePositionChange.Y)))
    End Sub

    Private Sub ApplyPlayerView()
        BuildButton.Surface = BuildAvailableSurface
        PropagandaButton.Surface = PropagandaAvailableSurface

        If (CanDeployWeapon) Then
            DeployWeaponButton.Surface = DeployWeaponAvailableSurface
        Else
            DeployWeaponButton.Surface = DeployWeaponUnavailableSurface
        End If

        If (CanDeployDefense) Then
            DeployDefenseButton.Surface = DeployDefenseAvailableSurface
        Else
            DeployDefenseButton.Surface = DeployDefenseUnavailableSurface
        End If

        If (CanBomb) Then
            BombButton.Surface = BombAvailableSurface
        Else
            BombButton.Surface = BombUnavailableSurface
        End If

        If (CanLaunchMissile) Then
            LaunchMissileButton.Surface = LaunchMissileAvailableSurface
        Else
            LaunchMissileButton.Surface = LaunchMissileUnavailableSurface
        End If
    End Sub

    Public Sub Dispose() Implements IMenu.Dispose
        BuildAvailableSurface.Dispose()
        BuildSelectedSurface.Dispose()
        PropagandaAvailableSurface.Dispose()
        PropagandaSelectedSurface.Dispose()
        DeployWeaponAvailableSurface.Dispose()
        DeployWeaponUnavailableSurface.Dispose()
        DeployWeaponSelectedSurface.Dispose()
        DeployDefenseAvailableSurface.Dispose()
        DeployDefenseUnavailableSurface.Dispose()
        DeployDefenseSelectedSurface.Dispose()
        BombAvailableSurface.Dispose()
        BombUnavailableSurface.Dispose()
        BombSelectedSurface.Dispose()
        LaunchMissileAvailableSurface.Dispose()
        LaunchMissileUnavailableSurface.Dispose()
        LaunchMissileSelectedSurface.Dispose()
    End Sub
End Class
